package zjitc.web.servlet;

import zjitc.domain.Test;
import zjitc.service.UserService;
import zjitc.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 设置编码
        request.setCharacterEncoding("utf-8");
        //2. 获取参数
        String name=request.getParameter("name");
        String sex = request.getParameter("gender");
        int age = Integer.parseInt(request.getParameter("age"));
        Test user = new Test();
        user.setName(name);
        user.setAge(age);
        user.setSex(sex);

        //3. 调用Service保存
        UserService service = new UserServiceImpl();
        service.addUser(user);

        //4. 跳转到userListServlet
        request.getRequestDispatcher("/userListServlet").forward(request,response);


    }
}
